/*
 * display.c
 *
 *  Created on: Dec 4, 2023
 *      Author: boen
 */
#include <display.h>

#define DISP_DELAY		1

static unsigned int HT16K33_Buf[]=
{	//提取段码为：0，1，2，3，4，5，6，7，8，9,空白,横线
	0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40
};
//static uint16_t disp_num1;
//static uint16_t disp_num2;
static uint8_t disp_step = 0;

//static unsigned int display_tick = 1;


/**
  * @brief
  * @param
  * @retval
  */
void display_timer(void)
{
//	if(display_tick)
//	{
//		display_tick--;
//	}
//	else
//	{
//		display_tick = 1;
//	}
}

/**
  * @brief
  * @param
  * @retval
  */
static inline void iic_start(void)
{
	HT16K33_SDA_H;
	HT16K33_SCL_H;
	delay_us(DISP_DELAY);
	HT16K33_SDA_L;
	delay_us(DISP_DELAY);
	HT16K33_SCL_L;
}


/**
  * @brief
  * @param
  * @retval
  */
static inline void iic_stop(void)
{
	HT16K33_SCL_H;
	delay_us(DISP_DELAY);
	HT16K33_SDA_L;
	delay_us(DISP_DELAY);
	HT16K33_SDA_H;
}


/**
  * @brief
  * @param
  * @retval
  */
static inline void iic_wait_ack(void) //测数据信号的电平
{
	HT16K33_SCL_H;
	delay_us(DISP_DELAY);
	HT16K33_SCL_L;
}


/**
  * @brief
  * @param
  * @retval
  */
static inline void iic_send_byte(uint8_t dat)
{
	uint8_t i;
	for(i=0;i<8;i++)
	{
		HT16K33_SCL_L;//将时钟信号设置为低电平
		if(dat&0x80)//将dat的8位从最高位依次写入
		{
			HT16K33_SDA_H;
			delay_us(DISP_DELAY);
		}
		else
		{
			HT16K33_SDA_L;
			delay_us(DISP_DELAY);
		}
		HT16K33_SCL_H;//将时钟信号设置为高电平
		delay_us(DISP_DELAY);
		HT16K33_SCL_L;//将时钟信号设置为低电平
		dat<<=1;
	}
}


/**
  * @brief
  * @param
  * @retval
  */
static inline void display_write_cmd(unsigned char cmd)
{

	iic_start();
	iic_send_byte(HT16K33_ADDRESS);//发送写器件地址
	iic_wait_ack();
	iic_send_byte(cmd);	//发送数据
	iic_wait_ack();
	iic_stop();

}


/**
  * @brief
  * @param
  * @retval
  */
static inline void display_write_data(unsigned char addr,unsigned char dat)
{
	iic_start();
	iic_send_byte(HT16K33_ADDRESS);//发送写器件地址
	iic_wait_ack();
	iic_send_byte(addr);//发送要写入内存地址
	iic_wait_ack();
	iic_send_byte(dat);	//发送数据
	iic_wait_ack();
	iic_stop();
}

/**
  * @brief
  * @param
  * @retval
  */
void display_num(unsigned char Place, unsigned int dat)
{

	display_write_data(Place*2,(unsigned char)dat);
	display_write_data(Place*2+1,(unsigned char)(dat>>8));
}


/**
  * @brief
  * @param
  * @retval
  */
static inline void display_brightness(unsigned char val)
{
	display_write_cmd(HT16K33_BRIGHTNESS | val);
}


/**
  * @brief
  * @param
  * @retval
  */
static inline void display_clear(void)
{
	unsigned char i;
	for (i=0; i<16; i++)
	{
		display_write_data(i,0);
	}
}


///**
//  * @brief
//  * @param
//  * @retval
//  */
//void display_set_number(uint16_t num1, uint16_t num2)
//{
//	disp_num1 = num1;
//	disp_num2 = num2;
//}


/**
  * @brief
  * @param
  * @retval
  */
void display_init(void)
{
	display_write_cmd(HT16K33_ON);
	display_write_cmd(HT16K33_DISPLAYON);
	display_brightness(BRILLIANCE);
	//display_set_number(000,000);
}


/**
  * @brief
  * @param
  * @retval
  */
void display_deinit(void)
{
	display_init();
	display_clear();
	display_brightness(0);
}


/**
  * @brief
  * @param
  * @retval
  */
void display_polling(void)
{

	switch(disp_step)
	{
		case 0:
			display_num(0,HT16K33_Buf[11]);
			disp_step = 1;
			break;
		case 1:
			display_num(1,HT16K33_Buf[xbot.camera.qrcode.t1_H]);
			disp_step = 2;
			break;
		case 2:
			display_num(2,HT16K33_Buf[xbot.camera.qrcode.t1_M]);
			disp_step = 3;
			break;
		case 3:
			display_num(3,HT16K33_Buf[xbot.camera.qrcode.t1_L]);
			disp_step = 4;
			break;
		case 4:
			display_num(4,HT16K33_Buf[11]);
			disp_step = 5;
			break;
		case 5:
			display_num(5,HT16K33_Buf[xbot.camera.qrcode.t2_H]);
			disp_step = 6;
			break;
		case 6:
			display_num(6,HT16K33_Buf[xbot.camera.qrcode.t2_M]);
			disp_step = 7;
			break;
		case 7:
			display_num(7,HT16K33_Buf[xbot.camera.qrcode.t2_L]);
			disp_step = 0;
			break;
		default:
			disp_step = 0;
			break;
	}
}
